package gui.toolbar;

import gui.MainWindow;
import gui.menubar.OpenFileMenuItem;
import gui.menubar.SaveMapMenuItem;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

import model.MetroModel;

public class FileToolBar extends JToolBar{
	public FileToolBar(MainWindow parent) {
		add(new NewMapButton());
		add(new OpenMapButton(parent));
		add(new SaveMapButton());
	}
	class OpenMapButton extends JButton {
		OpenMapButton(final MainWindow parent) {
			super("Open");
			setFocusable(false);
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					OpenFileMenuItem.showOpenFileDialog(FileToolBar.this);
					parent.getMainPanel().getFirstView().afterPanelInit();
					parent.getMainPanel().getSecondView().afterPanelInit();
				}
				
			});
		}
	}
	class SaveMapButton extends JButton {
		SaveMapButton() {
			super("Save");
			setFocusable(false);
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					SaveMapMenuItem.showSaveFileDialog(FileToolBar.this);
				}
				
			});
		}
	}
	class NewMapButton extends JButton {
		NewMapButton(){
			super("New map");
			setFocusable(false);
			addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if(MainWindow.askForSave("Do you want to crate new file without saving current?", NewMapButton.this)!=JOptionPane.CANCEL_OPTION)
						MetroModel.getInstance().clear();
					
				}
				
			});
		}
	}
	
}
